#pragma once

#include "Types.h"
#include "Bezier.h"
#include "DLL.h"
#include "DrawTool.h"
#include "SelectTool.h"
#include "ConvertTool.h"
#include "AddPointTool.h"
#include "DeletePointTool.h"

#include <MyViews/ZoomBench.h>
#include <MyFC/Event.h>

namespace My {

class DLL_API BezierEditor : 
	public CCmdTarget 
{
public:
	BezierEditor(ZoomBench &);
	~BezierEditor();
public:
	ZoomBench & GetBench()
		{ return m_Bench; }
	BezierOwner * GetBezierOwner() const
		{ return m_pOwner; }
	bool IsOpen() const
		{ return m_pOwner != 0; }
	DrawTool * Draw() const
		{ return &m_DrawTool; }
	SelectTool * Select() const
		{ return &m_SelectTool; }
	ConvertTool * Convert() const
		{ return &m_ConvertTool; }
	AddPointTool * AddPoint() const
		{ return &m_AddPointTool; }
	DeletePointTool * DeletePoint() const
		{ return &m_DeletePointTool; }
	int GetTolerance() const
		{ return m_Tolerance; }
public:
	void Open(BezierOwner &);
	void Close();
	void PaintRect(CDC *, const CRect &);
	void PaintRect(CDC *, const CRect &, CRect const & rImage, ByteImage const & im, bool ClipMask, COLORREF background);
	void Nudge(CPoint);
public:
	bool ControlPointAt(CPoint p, Size & icurve, Size & inode, Size & icontrol);
	bool NodeAt(CPoint p, Size & icurve, Size & inode);
	bool CurveAt(CPoint p, Size & icurve, float & t);
public:
	void DrawBezier(CDC *, const Bezier &, bool StrokePath);
	void DrawNodes(CDC *, const Bezier &);
	void DrawSmallNodes(CDC *, const Bezier &);
	void DrawNode(CDC *, const BezierNode &, bool selected, COLORREF = RGB(0, 0, 0));
	void DrawSmallNode(CDC *, const BezierNode &, COLORREF = RGB(0, 0, 0));
	void DrawHandles(CDC *, const Bezier &, int index);
	void DrawHandle(CDC *, const point<float> & p, const point<float> & c, COLORREF = RGB(0, 0, 0));
private:
	ZoomBench &		m_Bench;
	BezierOwner *	m_pOwner;
	Tool *			m_pTool;
	int				m_Tolerance;
	mutable DrawTool			m_DrawTool;
	mutable SelectTool			m_SelectTool;
	mutable ConvertTool			m_ConvertTool;
	mutable AddPointTool		m_AddPointTool;
	mutable DeletePointTool		m_DeletePointTool;
protected:
	DECLARE_MESSAGE_MAP()
public:
	void OnToolDraw();
	void OnUpdateToolDraw(CCmdUI * pCmdUI);
	void OnToolSelect();
	void OnUpdateToolSelect(CCmdUI * pCmdUI);
	void OnToolConvert();
	void OnUpdateToolConvert(CCmdUI * pCmdUI);
	void OnToolAddPoint();
	void OnUpdateToolAddPoint(CCmdUI * pCmdUI);
	void OnToolDeletePoint();
	void OnUpdateToolDeletePoint(CCmdUI * pCmdUI);
};

} // My
